package com.woniuxy.product.controller;

import com.woniuxy.product.pojo.Account;
import com.woniuxy.product.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController {

    @Value("${server.port}")
    private String port;

    /**
     * 传参
     * 1.路径传参：必须要加@PathVariable
     * 2.请求参数传参：必须要String msg 可选@RequestParam（“msg”）
     * 3.请求体传参：必须要@RequestBody
     * @param msg
     * @return
     */

    @RequestMapping("/getProduct/{msg}")
    public String getProduct(@PathVariable String msg) throws InterruptedException {
        if("1".equals(msg)){
            int i=1/0; //抛出异常
        }else if("2".equals(msg)){
            Thread.sleep(800);//休眠800毫秒
        }
        return "product"+msg + port;
    }

    @Autowired
    private AccountService accountService;

    @RequestMapping("/getByid/{id}")
    public Account getByid(@PathVariable Integer id){
        return accountService.getById(id);
    }

    @RequestMapping("/getList")
    public List<Account> getList(){
        return accountService.list();
    }

    @RequestMapping("/getbyCondition")
    public List<Account> getbyCondition(Integer id){
        //面向对象编程    面向过程    面向对象  面向接口编程（SPI:JDBC） 面向服务  面向方法
        return accountService.lambdaQuery()
                .eq(Account::getId,id)
                .list();
    }

    @RequestMapping("/updateAcc")
    public boolean updateAcc(Account account){
        return accountService.updateById(account);
    }

    @RequestMapping("/updateAccById")
    public boolean updateAccById(Account account){
        return accountService.lambdaUpdate()
                .eq(Account::getId,account.getId())
                .set(Account::getAccountName,account.getAccountName())
                .update();
    }

    @RequestMapping("/addAcc")
    public boolean addAcc(Account account){
        return accountService.save(account);
    }

    @RequestMapping("/delAccountById/{id}")
    public boolean delAccountById(@PathVariable Integer id){
        return accountService.removeById(id);
    }

    @RequestMapping("/delAccountByConditionId/{id}")
    public boolean delAccountByConditionId(@PathVariable Integer id){
        return accountService.lambdaUpdate()
                .eq(Account::getId,id)
                .remove();
    }

}
